<?php
/*
 * Uploadify
 * Copyright (c) 2012 Reactive Apps, Ronnie Garcia
 * Released under the MIT License <http://www.opensource.org/licenses/mit-license.php>
 */

// Define a destination
require_once '../../../extend/image.func.php';

$targetFolder = '/attached/' . date ( 'Ymd', time () ); // Relative to the root

$width = empty ( $_POST ['width'] ) ? 0 : $_POST ['width'];
$height = empty ( $_POST ['height'] ) ? 0 : $_POST ['height'];

if (! empty ( $_FILES )) {
	$tempFile = $_FILES ['file'] ['tmp_name'];
	$targetPath = $_SERVER ['DOCUMENT_ROOT'] . $targetFolder;
	if (! is_file ( $targetPath ))
		@mkdir ( $targetPath );
	// Validate the file type
	$fileTypes = array ('jpg','jpeg','gif','png' ); // File extensions
	$fileParts = pathinfo ( $_FILES ['file'] ['name'] );
	
	$new_file_name = time () . '.' . $fileParts ['extension'];
	$thumb_file_name = 'thumb_' . $new_file_name;
	$targetFile = rtrim ( $targetPath, '/' ) . '/' . $new_file_name;
	$thumbtargetFile = rtrim ( $targetPath, '/' ) . '/' . $thumb_file_name;
	if (in_array ( strtolower ( $fileParts ['extension'] ), $fileTypes )) {
		move_uploaded_file ( $tempFile, $targetFile );
		if ($width > 0) {
			move_uploaded_file ( $tempFile, $targetFile );
			make_thumb ( $targetFile, $thumbtargetFile, $width, $height, 80 );
			echo $targetFolder . '/' . $thumb_file_name;
		} else
			echo $targetFolder . '/' . $new_file_name;
	} else {
		echo 'Invalid file type.';
	}
}
?>